import numpy as np
import matplotlib.pyplot as plt

# 1.	完成数据集的读取（10分）
data = np.loadtxt('soap.txt', delimiter=',')
x = data[:, :-1]
y = data[:, -1]

# 2.	导入库函数及数据，将数据集分割成训练集和测试集。（20分）
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.7, random_state=666)

# 3.	用散点图可视化显示数据集(20分)
plt.scatter(x[:, 0], x[:, 1], c=y)

# 4.	建立适当模型(20分)
model = LogisticRegression(solver='liblinear')

# 5.	用训练集进行训练，然后对测试集预测 (20分)
model.fit(x_train, y_train)
print(model.predict(x_test))

# 6.	计算预测准确率及输出(10分)
print(f'训练集准确率:{model.score(x_train, y_train)}')
print(f'测试集准确率:{model.score(x_test, y_test)}')

# finally show all drawings
plt.show()
